package server.handler;

import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpsExchange;
import org.json.JSONObject;
import server.database.RegistrationDatabase;
import server.util.Base64Util;
import server.util.MD5Util;
import server.entity.Registration;

import java.io.IOException;
import java.sql.SQLException;



public class RegistrationHandler implements HttpHandler {
    private RegistrationDatabase db;
    public RegistrationHandler() {
        db = RegistrationDatabase.getInstance();
    }

    @Override
    public void handle(HttpExchange exchange) throws IOException {

        Headers headers = exchange.getRequestHeaders();
        String contentType = headers.get("Content-Type").get(0);
        if(contentType.equalsIgnoreCase("application/json")){
            if (exchange.getRequestMethod().equalsIgnoreCase("POST")) {
                String inputData;
                //get input Data
                try{
                    inputData = Handler.getRequestData(exchange);
                }catch (IOException e){
                    Handler.errorHandler(exchange, e);
                    return;
                }
                JSONObject jsonObject = new JSONObject(inputData);
                //attribute validation
                if(!jsonObject.has("username")){
                    Handler.errorHandler(exchange, "username is required");
                    return;
                }
                if(!jsonObject.has("password")){
                    Handler.errorHandler(exchange, "password is required");
                    return;
                }
                if(!jsonObject.has("email")){
                    Handler.errorHandler(exchange, "Email is required");
                    return;
                }

                //Check if username exists
                try {
                    if(db.hasUsername(jsonObject.getString("username"))){
                        Handler.errorHandler(exchange, "Username already exists");
                        return;
                    }
                } catch (SQLException e) {
                    Handler.errorHandler(exchange, e);
                    return;
                }

                //base64
                String password = "";
                try {
                    password = Base64Util.decryBASE64(jsonObject.getString("password"));
                } catch (Exception e) {
                    Handler.errorHandler(exchange, e);
                    return;
                }

                //Password at least 6 characters
//                if(password.length() < 6){
//                    Handler.errorHandler(exchange, "Password at least 6 characters");
//                    return;
//                }

                //MD5
                try{
                    password = MD5Util.encryMD5(password);
                }catch (Exception e){
                    Handler.errorHandler(exchange, e);
                    return;
                }

                //registered user
                Registration  registration = new Registration(
                        jsonObject.getString("username"),
                        password,
                        jsonObject.getString("email"));

                try {
                    db.storeRegistrations(registration);
                } catch (SQLException e) {
                    Handler.errorHandler(exchange, e);
                    return;
                }
                Handler.sendResponse(exchange, "register success");
            }else{
                //Code for handling other requests
                Handler.errorHandler(exchange, "Unsupported request method");
            }
        }
        else {
            Handler.errorHandler(exchange, "Unsupported request contentType");
        }
    }
}

